package com.wxzz.elearing.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wxzz.elearing.user.entity.AdminPermission;
import com.wxzz.elearing.user.entity.AdminUserRole;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author
 * @since 2021-03-11
 */
public interface AdminUserRoleMapper extends BaseMapper<AdminUserRole> {

    /**
     * 删除用户对应的角色
     *
     * @param adminUserId
     * @return
     */
    @Update("UPDATE admin_user_role SET deleted = 1 WHERE admin_user_id = #{adminUserId}")
    Integer deleteAdminUserRoleByAdminUserId(@Param("adminUserId") Long adminUserId);

    @Select("SELECT\n" +
            "\tp.* \n" +
            "FROM\n" +
            "\tadmin_role r\n" +
            "\tJOIN admin_role_permission rp ON r.id = rp.admin_role_id\n" +
            "\tJOIN admin_permission p ON p.id = rp.admin_permission_id \n" +
            "WHERE\n" +
            "\tr.id = #{adminRoleId} \n" +
            "\tAND pid = #{pid} AND r.deleted = 0 AND rp.deleted = 0 AND p.deleted = 0  ORDER BY sort ASC")
    List<AdminPermission> selectPermissionByRoleId(@Param("adminRoleId") Long adminRoleId, @Param("pid") Long pid);

    /**
     * 查询所有权限根据角色id
     * @param adminRoleId
     * @return
     */
    @Select("SELECT\n" +
            "\tp.* \n" +
            "FROM\n" +
            "\tadmin_role r\n" +
            "\tJOIN admin_role_permission rp ON r.id = rp.admin_role_id\n" +
            "\tJOIN admin_permission p ON p.id = rp.admin_permission_id \n" +
            "WHERE\n" +
            "\tr.id = #{adminRoleId} \n" +
            "\tAND r.deleted = 0 AND rp.deleted = 0 AND p.deleted = 0  ORDER BY sort ASC")
    List<AdminPermission> selectAllPermissionByRoleId(@Param("adminRoleId") Long adminRoleId);

    /**
     * 查询用户对应角色
     * @param adminUserId
     * @return
     */
    @Select("SELECT * FROM `admin_user_role` WHERE admin_user_id = #{adminUserId} AND deleted = 0")
    Optional<AdminUserRole> selectAdminUserRoleByAdminUserId(@Param("adminUserId") Long adminUserId);

}
